# coding : utf-8
# polygon用于表示车道附属物

from matplotlib.patches import Polygon
from matplotlib.lines import Line2D
import numpy as np

import dxfgrabber

import map_artist


class MapPolygon(Polygon):
    def __init__(self, xydata=np.zeros((0, 2)), closed=False, **kwargs):
        super().__init__(xydata, closed=False, **kwargs)
        self.belongs = list([])
        self.step = 0.5  # todo 暂时用固定值
        self.maptype = 'appendix'
        self.dxftype = None
        self.objecttype = None
        self.configed = False
        self.selected = False
        self.index = 0

    def fromDXFEntity(self, dxfentity, true_color):
        self.dxftype = dxfentity.dxftype
        self.set_color(true_color)
        self.true_color = true_color
        if self.dxftype == 'ARC':
            self.start_angle = dxfentity.start_angle * np.pi / 180
            self.end_angle = dxfentity.end_angle * np.pi / 180
            self.radius = dxfentity.radius
            self.center = dxfentity.center
            if self.start_angle > self.end_angle:
                self.start_angle -= np.pi * 2
            angle_step = self.step / self.radius
            angle_list = np.arange(self.start_angle, self.end_angle,
                                   angle_step)
            angle_list = np.append(angle_list, self.end_angle)
            x = self.radius * np.cos(angle_list) + self.center[0]
            y = self.radius * np.sin(angle_list) + self.center[1]
            data = np.vstack((x, y))
            self.xy = data
        elif self.dxftype == 'CIRCLE':
            self.start_angle = 0
            self.end_angle = 2 * np.pi
            self.radius = dxfentity.radius
            self.center = dxfentity.center
            angle_step = self.step / self.radius
            angle_list = np.arange(self.start_angle, self.end_angle,
                                   angle_step)
            angle_list = np.append(angle_list, self.end_angle)
            x = self.radius * np.cos(angle_list) + self.center[0]
            y = self.radius * np.sin(angle_list) + self.center[1]
            data = np.vstack((x, y)).reshape(-1, 2)
            self.xy = data
        # elif self.dxftype == 'POINT':
        #     self.start, self.end = dxfentity.point, dxfentity.point
        #     self.slength = 0
        #     self.set_xdata(self.start[0])
        #     self.set_ydata(self.start[1])
        else:
            data = np.array(dxfentity.points)
            self.xy = data

    def set_index(self, index):
        self.index = index
        self.set_url(index)

    def get_index(self):
        return self.index

    def setSelected(self):
        self.selected = True
        self.set_alpha(0.3)

    def setDeselected(self):
        self.selected = False
        self.set_alpha(1)

    def setMapType(self, maptype):
        """
        'traffice_light', 'lidar_sign', 'camera_sign', 'charger', 'obstacle'
        """
        self.maptype = maptype

    def setObjectParams(self, **kwargs):
        """
        # todo
        """
        # self.params = **kwargs
        return

    def setBelongs(self, belongs_id):
        self.belongs = belongs_id

    def clearBelongs(self, belongs):
        self.belongs = list([])

    def setDefaultAlpha(self):
        self.set_alpha(1)

    def setSelectedAlpha(self):
        self.set_alpha(0.3)

    def toggleAlpha(self):
        if self.selected:
            self.setSelectedAlpha()
        else:
            self.setDefaultAlpha()

    def toggleSelected(self):
        self.selected = not self.selected

    def setObjectType(self, objecttype):
        self.objecttype = objecttype

    def clearObjectType(self):
        self.objecttype = None

    def setConfiged(self):
        self.configed = True
        self.set_color('red')

    def setReconfig(self):
        self.configed = False
        self.set_color(self.true_color)

    def toAppendixAttributeStr(self):
        str_list = []
        str_list.append('{}'.format(self.index))  # index
        str_list.append('{}'.format(len(self.belongs)))  # belongs_number
        for i in self.belongs:
            str_list.append(str(i))
        str_list.append('{}'.format(self.objecttype))  # type
        xy_data = self.get_xy()
        print('appendix_attribute', xy_data)
        str_list.append('{}'.format(len(xy_data)))
        for xy in xy_data:
            str_list.append('{}'.format(xy[0]))
            str_list.append('{}'.format(xy[1]))

        # [xmin_i, ymin_i] = np.argmin(xy_data, axis=0)
        # [xmax_i, ymax_i] = np.argmax(xy_data, axis=0)

        # str_list.append('{}'.format(xy_data[xmin_i, 0]))
        # str_list.append('{}'.format(xy_data[xmin_i, 1]))
        # str_list.append('{}'.format(xy_data[xmax_i, 0]))
        # str_list.append('{}'.format(xy_data[xmax_i, 1]))
        # str_list.append('{}'.format(xy_data[ymin_i, 0]))
        # str_list.append('{}'.format(xy_data[ymin_i, 1]))
        # str_list.append('{}'.format(xy_data[ymax_i, 0]))
        # str_list.append('{}'.format(xy_data[ymax_i, 1]))
        str_value = ','.join(str_list)
        return str_value

    def fromAppendixAttributeStr(self, str_value):
        value = str_value.split(',')
        belongs_number = int(value[1])
        self.belongs = list(
            int(ref_id) for ref_id in value[2:2 + belongs_number])
        print('self.belongs', self.belongs)
        self.objecttype = int(value[1 + belongs_number + 1])
        print('self.objecttype', self.objecttype)


if __name__ == "__main__":
    pass
